package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"weekly-exam/api/request"
	"weekly-exam/globar"
	"weekly-exam/intermal/dao"
	"weekly-exam/intermal/model"
	"weekly-exam/pkg"
)

func Videos(c *gin.Context) {
	var req request.VideoReq
	var err error
	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "验证失败",
			"data":    false,
		})
		return
	}
	var video model.Video
	dao.GetOneFile(&model.Video{Info: req.Info}, &video)
	res := pkg.Check(req.Name)
	if res == false {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "触发敏感词",
			"data":    false,
		})
		return
	}
	video = model.Video{
		UserId: int32(c.GetUint("userId")),
		Info:   req.Info,
		Image:  req.Image,
		Name:   req.Name,
	}
	dao.Create(&video)
	if video.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusBadRequest,
			"message": "添加失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "成功",
		"data":    true,
	})
}

func List(c *gin.Context) {
	var video []model.Video
	globar.DB.Find(&video)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查看成功",
		"data":    video,
	})
}
